package STUDIO.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import STUDIO.excepcion.DAOExcepcion;
import STUDIO.excepcion.DAOStudioExcepcion;
import STUDIO.excepcion.LoginStudioExcepcion;
import STUDIO.modelo.Clientes;
import STUDIO.modelo.administrador;
import STUDIO.util.CONEXIONStudioBD;

public class administradorDAO extends BaseStudioDAO {

	public administrador insertar(administrador vo) throws DAOStudioExcepcion {

		System.out.println("Insertando datos en administrador");
		String query = "INSERT INTO administrador (DNI,nombres,email,fecha_nac,local_idlocal) VALUES (?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			con = CONEXIONStudioBD.obtenerConexion();
	
			stmt = con.prepareStatement(query);
		
			stmt.setInt(1,vo.getDni());
			stmt.setString(2,vo.getNombres());
			stmt.setString(3,vo.getEmail());
			stmt.setString(4,vo.getFecha_nac());
			stmt.setInt(5,vo.getLocal().getidlocal());
			System.out.print("Ingresado el administrador");
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			
			int idad = 0;
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				idad = rs.getInt(1);
			}
			vo.setIdadmin(idad);
		
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				throw new DAOStudioExcepcion(e.getMessage());
			}
			System.err.println(e.getMessage());
			throw new DAOStudioExcepcion(e.getMessage());
		} finally {
			try {
				con.setAutoCommit(true);
			} catch (SQLException e) {
				throw new DAOStudioExcepcion(e.getMessage());
			}
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	
	

}
